package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.dto.FinanceOrderRefundmentExamineDTO;
import com.ktgj.customer.domain.vo.FinanceOrderRefundmentVo;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.service.IFinanceOrderRefundmentService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 财务会员退款信息Controller
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@RestController
@RequestMapping("/refundment")
public class FinanceOrderRefundmentController extends BaseController
{
    @Autowired
    private IFinanceOrderRefundmentService financeOrderRefundmentService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询财务会员退款信息列表
     */
    @PreAuthorize(hasPermi = "customer:refundment:list")
    @GetMapping("/list")
    public TableDataInfo list(FinanceOrderRefundment financeOrderRefundment)
    {
        startPage();
        List<FinanceOrderRefundment> list = financeOrderRefundmentService.selectFinanceOrderRefundmentList(financeOrderRefundment);
        return getDataTable(list);
    }

    /**
     * 导出财务会员退款信息列表
     */
    @PreAuthorize(hasPermi = "customer:refundment:export")
    @Log(title = "财务会员退款信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceOrderRefundment financeOrderRefundment) throws IOException
    {
        List<FinanceOrderRefundment> list = financeOrderRefundmentService.selectFinanceOrderRefundmentList(financeOrderRefundment);
        ExcelUtil<FinanceOrderRefundment> util = new ExcelUtil<FinanceOrderRefundment>(FinanceOrderRefundment.class);
        util.exportExcel(response, list, "财务会员退款信息数据");
    }

    /**
     * 获取财务会员退款信息详细信息
     */
    @PreAuthorize(hasPermi = "customer:refundment:query")
    @GetMapping(value = "/{refundmentId}")
    public AjaxResult getInfo(@PathVariable("refundmentId") Long refundmentId)
    {
        return AjaxResult.success(financeOrderRefundmentService.selectFinanceOrderRefundmentById(refundmentId));
    }

    /**
     * 新增财务会员退款信息
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FinanceOrderRefundment financeOrderRefundment)
    {
        return toAjax(financeOrderRefundmentService.insertFinanceOrderRefundment(financeOrderRefundment));
    }

    /**
     * 新增财务会员退款信息(远程)
     */
    @PostMapping("/save")
    public int save(@RequestBody FinanceOrderRefundment financeOrderRefundment) {
        int i = financeOrderRefundmentService.insertFinanceOrderRefundment(financeOrderRefundment);
        return i;
    }

    /**
     * 提交审核的批次号顺序递增
     * */
    @PostMapping("/number")
    public int number(){
        int i = financeOrderRefundmentService.number();
        return i;
    }

    /**
     * 修改财务会员退款信息
     */
    @PreAuthorize(hasPermi = "customer:refundment:edit")
    @Log(title = "财务会员退款信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinanceOrderRefundment financeOrderRefundment)
    {
        return toAjax(financeOrderRefundmentService.updateFinanceOrderRefundment(financeOrderRefundment));
    }

    /**
     * 删除财务会员退款信息
     */
    @PreAuthorize(hasPermi = "customer:refundment:remove")
    @Log(title = "财务会员退款信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{refundmentIds}")
    public AjaxResult remove(@PathVariable Long[] refundmentIds)
    {
        return toAjax(financeOrderRefundmentService.deleteFinanceOrderRefundmentByIds(refundmentIds));
    }

    /**
     * 会员退款申请列表
     * @return
     */
    @PostMapping("/financeOrderRefundmentList")
    public TableDataInfo financeOrderRefundmentList(@RequestBody FinanceOrderRefundmentVo financeOrderRefundmentVo) {
        startPage();
        List<FinanceOrderRefundmentList> financeOrderRefundmentLists = financeOrderRefundmentService.financeOrderRefundmentList(financeOrderRefundmentVo);
        return getDataTable(financeOrderRefundmentLists);
    }

    /**
     * 退款申请确认
     * @param map
     * @return
     */
    @PostMapping("/orderRefundmentDetails")
    public AjaxResult orderRefundmentDetails(@RequestBody Map<String, Long> map) {
        Long refundmentId = map.get("refundmentId");
        FinanceOrderRefundmentDetails refundmentDetails = financeOrderRefundmentService.orderRefundmentDetails(refundmentId);
        return AjaxResult.success(refundmentDetails);
    }

    /**
     * 确认或取消退款申请
     * @param refundmentDetails
     * @return
     */
    @PostMapping("/updateOrderRefundment")
    public AjaxResult updateOrderRefundment(HttpServletRequest request, @RequestBody FinanceOrderRefundmentDetails refundmentDetails) {
        Employee loginEmployee;
        try {
            loginEmployee = loginService.getLoginEmployee(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (loginEmployee != null) {
            return toAjax(financeOrderRefundmentService.updateOrderRefundment(loginEmployee, refundmentDetails));
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 会员退款审核列表
     * @return
     */
    @PostMapping("/orderRefundmentExamine")
    public TableDataInfo orderRefundmentExamine(@RequestBody FinanceOrderRefundmentExamineDTO examineDTO) {
        startPage();
        List<FinanceOrderRefundmentExamine> refundmentExamine = financeOrderRefundmentService.orderRefundmentExamine(examineDTO);
        return getDataTable(refundmentExamine);
    }

    /**
     * 修改退款审核
     * @param map
     * @return
     */
    @PostMapping("/updateRefundmentExamine")
    public AjaxResult updateRefundmentExamine(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Employee loginEmployee;
        try {
            loginEmployee = loginService.getLoginEmployee(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (loginEmployee != null) {
            List<Object> refundmentId = (List<Object>) map.get("refundmentId");
            boolean isSuccess = (boolean) map.get("isSuccess");
            return toAjax(financeOrderRefundmentService.updateRefundmentExamine(loginEmployee, isSuccess, refundmentId));
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }

    /**
     * 出纳员完成退款列表
     * @param map
     * @return
     */
    @PostMapping("/completesRefundList")
    public AjaxResult completesRefundList(@RequestBody Map<String, String> map) {
        String reviewedTime = map.get("reviewedTime");
        String reviewedTimeEnd = map.get("reviewedTimeEnd");
        String employeeIdApply = map.get("employeeIdApply");
        String plfkformName = map.get("plfkformName");
        List<CompletesRefundList> refundLists = financeOrderRefundmentService.completesRefundList(reviewedTime, reviewedTimeEnd, employeeIdApply, plfkformName);
        return AjaxResult.success(refundLists);
    }

    /**
     * 批量付款列表
     * @param map
     * @return
     */
    @PostMapping("/bulkPayment")
    public AjaxResult bulkPayment(@RequestBody Map<String, Object> map) {
        Long batchNumber = Long.valueOf(map.get("batchNumber").toString());
        String reviewedTime = String.valueOf(map.get("reviewedTime"));
        String reviewedTimeEnd = String.valueOf(map.get("reviewedTimeEnd"));
        String orderId = String.valueOf(map.get("orderId"));
        List<FinanceOrderRefundmentExamine> examineList = financeOrderRefundmentService.bulkPayment(reviewedTime, reviewedTimeEnd, orderId, batchNumber);
        return AjaxResult.success(examineList);
    }

    /**
     * 退款确认
     * @param map
     * @return
     */
    @PostMapping("/refundConfirm")
    public AjaxResult refundConfirm(@RequestBody Map<String, Long> map) {
        Long refundmentId = map.get("refundmentId");
        FinanceOrderRefundmentExamine refundConfirm = financeOrderRefundmentService.refundConfirm(refundmentId);
        return AjaxResult.success(refundConfirm);
    }

    /**
     * 确认（及批量确认）或暂不退款
     * @param request
     * @param refundmentExamine
     * @return
     */
    @PostMapping("/isRefundConfirm")
    public AjaxResult isRefundConfirm(HttpServletRequest request, @RequestBody FinanceOrderRefundmentExamine refundmentExamine) {
        Employee loginEmployee;
        try {
            loginEmployee = loginService.getLoginEmployee(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (loginEmployee != null) {
            return toAjax(financeOrderRefundmentService.isRefundConfirm(loginEmployee, refundmentExamine));
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }
}
